<template>
   <div class="app-container">
      <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
         <!-- <el-form-item label="系统" prop="systemId">
            <el-select v-model="queryParams.systemId" placeholder="请选择所属系统" @change="getList" style="width: 200px">
               <el-option v-for="sys in dataSystemOptions" :label="sys.name" :value="sys.id" />
            </el-select>
         </el-form-item> -->
         <el-form-item label="SQL检索" prop="searchSql">
            <el-input v-model="queryParams.searchSql" placeholder="SQL检索"></el-input>
         </el-form-item>
         <el-form-item label="物模型" prop="tableId">
            <el-select v-model="queryParams.tableId" placeholder="请选择物模型" @change="getList" style="width: 200px">
               <el-option v-for="table in tableOptions" :label="table.name" :value="table.id" />
            </el-select>
         </el-form-item>
         <el-form-item label="执行时间">
            <el-date-picker
               v-model="dateRange"
               value-format="YYYY-MM-DD"
               type="daterange"
               range-separator="-"
               start-placeholder="开始日期"
               end-placeholder="结束日期"
            ></el-date-picker>
         </el-form-item>
         <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
         </el-form-item>
      </el-form>

      <el-table v-loading="loading" :data="dataList" row-key="id" @selection-change="handleSelectionChange">
         <el-table-column
            v-for="head in headList"
            :key="head.id"
            :prop="head.name"
            :label="head.comment || head.name"
            show-overflow-tooltip
            min-width="110"
         />
      </el-table>
      <pagination
         v-show="total > 0"
         :total="total"
         v-model:page="queryParams.pageNum"
         v-model:limit="queryParams.pageSize"
         @pagination="getList"
      />
   </div>
</template>

<script setup name="Menu">
import { ref, watch } from "vue";
import {
   dynamicGetTableBody,
   dynamicGetTableHead,
   dynamicGetSystemOption,
   dynamicGetModelOption
} from "@/api/dynamic";

const { proxy } = getCurrentInstance();
const headList = ref([]);
const dataList = ref([]);
const loading = ref(false);
const showSearch = ref(true);
const isExpandAll = ref(false);
const refreshTable = ref(true);
const total = ref(0);
const dateRange = ref([]);

const data = reactive({
   form: {},
   queryParams: {
      pageNum: 1,
      pageSize: 10,
      // systemId: null,
      searchSql: '',
      tableId: null,
   },
   detail: {}
});

const { queryParams, form, detail, rules } = toRefs(data);

/** 查询系统列表 */
function getList() {
   if(queryParams.value.tableId){
      loading.value = true;
      let complete = false
      const [beginTime = null, endTime = null] = dateRange.value
      dynamicGetTableHead({
         ...queryParams.value,
         beginTime: beginTime ? beginTime + ' 00:00:00' : null,
         endTime: endTime ? endTime + ' 23:59:59' : null,
      }).then(response => {
         headList.value = response.data;
         if(complete){
            loading.value = false
            complete = false
         } else {
            complete = true
         }
      });
      dynamicGetTableBody({...queryParams.value, beginTime, endTime}).then(response => {
         dataList.value = response.data.records;
         total.value = response.data.total;
         if(complete){
            loading.value = false
            complete = false
         } else {
            complete = true
         }
      });
   }
}

// const dataSystemOptions = ref([]);
// /** 获取系统选项 */
// function getDataSystemOption() {
//    dynamicGetSystemOption().then(response => {
//       dataSystemOptions.value = response.data;
//    });
// }

const tableOptions = ref([]);
/** 获取物模型选项 */
function getTableOption() {
   tableOptions.value = [];
   dynamicGetModelOption().then(response => {
      tableOptions.value = response.data;
   });
}

// watch(() => queryParams.value.systemId, val => {
//    queryParams.value.tableId = null
//    if(val) getTableOption(val)
// })

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  dateRange.value = [];
  headList.value = []
  dataList.value = []
  total.value = 0;
  proxy.resetForm("queryRef");
  handleQuery();
}

getTableOption()
</script>
<style lang='scss' scoped></style>